/**
 *	Monopolymorphism - A Java-based monopoly!
 *
 *	The final project of 
 *	Object-Oriented Programming Design of NTUCSIE
 *
 *	Authors:
 *		ChungKingSing.
 *		Qing-Cheng Li.
 */
package monopolymorphism;

import java.util.ArrayList;
import java.util.Scanner;

public class Player{
	
	/**	If this player is in jail, then this value is true,
	 *	otherwise this value is false.
	 */
	protected boolean inJail;
	protected int inJailDay;
	
	/** A unique number to identify player.
	*/
	private int id;
	
	/**	Name of player.
	*/
	private String name;
		
	/**	The money this player have.
	*/
	private int money;
	
	/** oops?
	*/
	public String color;
	
	/**	An ArrayList that contain the properties a player have.
	*/
	private ArrayList<Property> ownedProperty;
	
	/**	An ArrayList that contain the cards a player have.
	*/
	private ArrayList<Card> ownedCard;
	
	/** Construct of this player.Initialize this player.
	*/
	
	protected boolean isBankruptcy;
	protected boolean reverse;
	protected int location;
	
	public Player(int _id,String _name,int _money,boolean _inJail,int _ijd,
		ArrayList<Property> op,ArrayList<Card> oc,boolean _isB,boolean _r,int _lo){
		
		id = _id;
		name = _name;
		money = _money;
		inJail = _inJail;
		inJailDay = _ijd;
		ownedProperty = op;
		ownedCard = oc;
		isBankruptcy = _isB;
		reverse = _r;
		location = _lo;
		
	}
	
	/** Player can build house.
	*/
	public void build(){
		if(ownedProperty.size()==0){
			System.out.println("You have no lands");
			return;
		}
		int n =	showNormalLand();
		if(n==0)
			return;
		Scanner jin = new Scanner(System.in);
		n = jin.nextInt();
		NormalProperty np= (NormalProperty)ownedProperty.get(n);
		if(money<np.houseCost||np.level>=5){
			
		}else{
			money -= np.houseCost;
			np.level++;
		}
	}
	
	public void showAllLand(){
		showNormalLand();
		showAirportLand();
	}
	
	public int showAirportLand(){
		if(ownedProperty.size()==0){
			System.out.println("You have no Airport Land.");
			return 0;
		}
		Property tp;
		int sum = 0;
		AirportProperty ap;
		System.out.println("Airport:");
		for(int i=0;i<ownedProperty.size();i++){
			tp = ownedProperty.get(i);
			if(tp.type=="Airport"){
				ap = (AirportProperty)tp;
				sum++;
				System.out.println(i+") "+ap.name);
			}
		}
		if(sum==0)
			System.out.println("You have no Airport Land.");
		return sum;
	}
	
	public int showNormalLand(){
		if(ownedProperty.size()==0){
			System.out.println("You have no Normal Land.");
			return 0;
		}
		Property tp;
		int sum = 0;
		NormalProperty np;
		System.out.println("Normal:");
		for(int i=0;i<ownedProperty.size();i++){
			tp = ownedProperty.get(i);
			if(tp.type=="Normal"){
				np = (NormalProperty)tp;
				sum++;
				System.out.println(i+") ("+np.block+")"+np.name+" "+np.houseCost+" level="+np.level);
			}
		}
		if(sum==0)
			System.out.println("You have no Normal Land.");
		return sum;
	}
	
	public void showCard(){
		if(ownedCard.size()==0){
			System.out.println("You have no Card.");
			return;
		}
		for(int i=0;i<ownedCard.size();i++){
			System.out.println(i+") Card!");
		}
	}
	
	/**	Get how much the player have.
		@return int how much money the player have.
	*/
	public int getMoney(){
		return money;
	}
	
	public String getName(){
		return name;
	}
	
	public int getId(){
		return id;
	}
	
	/**	Add money to this player.
	 *	@param x the money will add to this player. 	
	 */
	public void addMoney(int x){
		this.money += x;
	}
	
	/**	The player pay money to another player.
		@param x
		@param y
	*/
	public void pay(int x,Player y){
		y.addMoney(x);
		this.money -= x;
	}
	
	/**	Buy a land on(?) the map.
		@param propetry
	*/
	public void buy(Property p){
		if(money<p.price){
			System.out.println("You CANNOT buy this land...");
		}else{
			p.owner = this;
			ownedProperty.add(p);
			System.out.println("You buy the land,"+p.name+" with "+p.price);
			this.money -= p.price;
		}
	}
	
	/**	Check is this player bankruptcy.
		@param x
		@return 
	*/
	public boolean bankruptcy(int x){
		
		if(this.money>=x){
			return false;
		}
		
		if(ownedProperty.isEmpty())
			return true;
			
		int sum;
		for(int i=0;i<ownedProperty.size();i++){
			this.money += ownedProperty.get(i).sell;
			ownedProperty.get(i).owner = null;
			//變賣訊息！
			ownedProperty.remove(i);
			if(this.money>=x)
				return false;
			if(ownedProperty.size()==0)
				break;
			i--;
		}
		return true;
		
	}
}
